﻿using System;
using System.Linq;
using System.Web.Mvc;
using Inscriptionem.Areas.Inscriptionem.Models.Requests;
using Inscriptionem.Areas.Inscriptionem.Models.Views;
using Inscriptionem.Authorization;
using Inscriptionem.Controllers;
using Inscriptionem.Extensions;

namespace Inscriptionem.Areas.Inscriptionem.Controllers
{
    [InscriptionemAuthorize(MemberRole.Admin, MemberRole.Moderator)]
    public class CommentController : Controller
    {
        private readonly ORM.DB DB;
        public CommentController()
        {
            DB = new ORM.DB();
        }

        public ActionResult Index()
        {
            return View("Index", new CommentCollectionViewModel());
        }

        [HttpPost]
        public ActionResult Approve(int ID, int Page)
        {
            try
            {
                var db = new ORM.DB();

                var comment = db.Comments.FirstOrDefault(c => c.ID == ID);
                if (comment != null)
                {
                    comment.IsPublished = true;
                    comment.ModifiedDate = DateTime.Now;

                    db.SaveChanges();
                }
            }
            catch (Exception)
            {

            }

            return View("Index", new CommentCollectionViewModel() { Page = Page });
        }

        [HttpPost]
        public ActionResult Decline(int ID, int Page)
        {
            try
            {
                var db = new ORM.DB();

                var comment = db.Comments.FirstOrDefault(c => c.ID == ID);
                if (comment != null)
                {
                    comment.IsDeleted = true;
                    comment.ModifiedDate = DateTime.Now;

                    db.SaveChanges();
                }
            }
            catch (Exception)
            {

            }

            return View("Index", new CommentCollectionViewModel() { Page = Page });
        }

        [HttpPost]
        public ActionResult Edit(CommentRequestModel model)
        {
            var result = new CommentViewModel();
            model.CopyTo(result);

            // Not the best way to check if "Edit" button was pressed
            if (result.ID > 0 && string.IsNullOrWhiteSpace(result.Content))
            {
                var comment = DB.Comments.FirstOrDefault(c => c.ID == result.ID);
                if (comment == null)
                {
                    // Comment not found
                    return RedirectToAction("Index");
                }

                result.Content = comment.Content;
                result.Member = comment.Member;
                result.IsPublished = comment.IsPublished;
                result.IsDeleted = comment.IsDeleted;

                return View("Edit", result);
            }

            // Save changes
            try
            {
                var comment = DB.Comments.FirstOrDefault(c => c.ID == result.ID);

                if (comment == null)
                {
                    return RedirectToAction("Index");
                }

                comment.Content = result.Content;
                comment.IsPublished = result.IsPublished;
                comment.IsDeleted = result.IsDeleted;

                DB.SaveChanges();

                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                result.Error = ex.ToString();
            }

            return View("Edit", result);
        }

        [HttpPost]
        public ActionResult Search(string Text, int Page)
        {
            if (Page < 0)
            {
                Page = 0;
            }

            var result = new CommentSearchCollectionViewModel { Page = Page, Text = Text };

            return View("Search", result);
        }

        public ActionResult Page(int ID)
        {
            if (ID <= 0)
            {
                return RedirectToAction("Index");
            }

            return View("Index", new CommentCollectionViewModel() { Page = ID });
        }

        #region Pagination
        public ActionResult Pagination(global::Inscriptionem.Models.Menu.Requests.PaginationRequestModel model)
        {
            return new MenuController().Pagination(model);
        }
        #endregion
    }
}
